import React, { useEffect, useRef, useState } from 'react';
import {
  View,
  StyleSheet,
  ImageBackground,
  TouchableOpacity,
  Text,
  Platform,
  Alert,
  ActivityIndicator,
  FlatList,
  Image,
  RefreshControl,
  ScrollView
} from 'react-native';
import SegmentedControlTab from "react-native-segmented-control-tab";
import { ItemHRow, SettingScrollView, SeekBar } from '../compoment';
import { Buffer } from 'buffer';
import { compose } from 'redux';
import { connect } from 'react-redux';
import { disConnectDev, updateAllData } from '../store/device/actions';
import { bleModule, bleProtocol } from './bleConst';
import { showMessage, hideMessage } from 'react-native-flash-message';
import { tcType } from '../utils/type';
import { settingMap } from '../utils/devicesType';

function SetPage({ navigation, devName,
  deviceId, allData, kmStr }) {

  const [selectedIndex, setSelectedIndex] = useState(0);

  // 让页面刷新
  const [isTest, setIsTest] = useState(false);

  // useEffect(() => {
  //   console.log('循环写入/n');
  // }, []);

  const crc16 = (data) => {
    let crcValue = 0xFFFF;
    for (let i = 0; i < data.length; i++) {
      crcValue ^= data[i] & 0xFFFF
      for (let j = 0; j < 8; j++) {
        if (crcValue & 0x0001) {
          crcValue >>= 1
          crcValue ^= 0xA001
        } else {
          crcValue >>= 1
        }
      }
    }
    crcValue = (Array(4).join('0') + crcValue.toString(16)).slice(-4);
    let crcArr = new Array(2)
    crcArr[0] = crcValue.substring(2, 4)
    crcArr[1] = crcValue.substring(0, 2)

    return crcArr;
  }

  React.useLayoutEffect(() => {
    navigation.setOptions({
      headerLeft: () => (
        <TouchableOpacity
          onPress={() => { }}
          style={{ paddingHorizontal: 15 }}>
          {
            deviceId ? <View>
              <Image
                source={require('../images/left_blue.png')}
                style={{ height: 24, width: 24 }}
              />
            </View> : null
          }
        </TouchableOpacity>
      ),
      headerRight: () => (
        <TouchableOpacity
          onPress={() => { }}
          style={{ paddingHorizontal: 15 }}>
          {
            deviceId ? <View style={{ flexDirection: 'row', alignItems: 'center' }}>
              <Text>{devName}</Text>
              {/* <Image
                source={require('../images/right_edit.png')}
                style={{ height: 24, width: 24 }}
              /> */}
            </View> : null
          }

        </TouchableOpacity>
      ),
    });
  }, [navigation, deviceId]);

  function sliderWithIndex(index, newNumber) {
    if (selectedIndex == 0) {
      switch (index) {
        case 0:
          // 速度
          if (newNumber == 100) {
            allData[1][11] = 0;
            allData[1][12] = 0;
            allData[1][13] = 0;
            allData[1][14] = 1;
          } else {
            allData[1][11] = (newNumber * 2.56).toFixed(0);
            allData[1][13] = 0;
            allData[1][14] = 0;
          }
          break;
        case 1:
          if (newNumber == 100) {
            allData[1][7] = 0;
            allData[1][8] = 0;
            allData[1][9] = 0;
            allData[1][10] = 1;
          } else {
            allData[1][7] = (newNumber * 2.56).toFixed(0);
            allData[1][9] = 0;
            allData[1][10] = 0;
          }
          break;
        case 2:
          if (newNumber == 100) {
            allData[1][3] = 0;
            allData[1][4] = 0;
            allData[1][5] = 0;
            allData[1][6] = 1;
          } else {
            allData[1][3] = (newNumber * 2.56).toFixed(0);
            allData[1][5] = 0;
            allData[1][6] = 0;
          }
          // console.log('allData[1][3]:' + allData[1][3]);
          // allData[1][3] = 128; 
          break;
        case 3:
          if (newNumber == 100) {
            allData[1][15] = 0;
            allData[1][16] = 0;
            allData[1][17] = 0;
            allData[1][18] = 1;
          } else {
            allData[1][15] = (newNumber * 2.56).toFixed(0);
            allData[1][17] = 0;
            allData[1][18] = 0;
          }
          // allData[1][16] = newNumber;
          break;
        case 4:
          if (newNumber == 100) {
            allData[1][19] = 0;
            allData[1][20] = 0;
            allData[1][21] = 0;
            allData[1][22] = 1;
          } else {
            allData[1][19] = (newNumber * 2.56).toFixed(0);
            allData[1][21] = 0;
            allData[1][22] = 0;
          }
          // allData[1][20] = newNumber;
          break;
        case 5:
          // 修改曲线类型  将值传给曲线组件  曲线显示更新
          allData[1][31] = newNumber;


          allData[1][35] = parseInt(tcType[newNumber][0] * 655.36 / 256);
          allData[1][36] = parseInt(tcType[newNumber][0] * 655.36 % 256);
          allData[1][37] = 0;
          allData[1][38] = 0;

          allData[1][39] = parseInt(tcType[newNumber][1] * 655.36 / 256);
          allData[1][40] = parseInt(tcType[newNumber][1] * 655.36 % 256);
          allData[1][41] = 0;
          allData[1][42] = 0;

          allData[1][43] = parseInt(tcType[newNumber][2] * 655.36 / 256);
          allData[1][44] = parseInt(tcType[newNumber][2] * 655.36 % 256);
          allData[1][45] = 0;
          allData[1][46] = 0;

          allData[1][47] = parseInt(tcType[newNumber][3] * 655.36 / 256);
          allData[1][48] = parseInt(tcType[newNumber][3] * 655.36 % 256);
          allData[1][49] = 0;
          allData[1][50] = 0;


          allData[2][3] = parseInt(tcType[newNumber][4] * 655.36 / 256);
          allData[2][4] = parseInt(tcType[newNumber][4] * 655.36 % 256);
          allData[2][5] = 0;
          allData[2][6] = 0;

          allData[2][7] = parseInt(tcType[newNumber][5] * 655.36 / 256);
          allData[2][8] = parseInt(tcType[newNumber][5] * 655.36 % 256);
          allData[2][9] = 0;
          allData[2][10] = 0;

          allData[2][11] = parseInt(tcType[newNumber][6] * 655.36 / 256);
          allData[2][12] = parseInt(tcType[newNumber][6] * 655.36 % 256);
          allData[2][13] = 0;
          allData[2][14] = 0;

          allData[2][15] = parseInt(tcType[newNumber][7] * 655.36 / 256);
          allData[2][16] = parseInt(tcType[newNumber][7] * 655.36 % 256);
          allData[2][17] = 0;
          allData[2][18] = 0;

          allData[2][19] = parseInt(tcType[newNumber][8] * 655.36 / 256);
          allData[2][20] = parseInt(tcType[newNumber][8] * 655.36 % 256);
          allData[2][21] = 0;
          allData[2][22] = 0;

          allData[2][23] = parseInt(tcType[newNumber][9] * 655.36 / 256);
          allData[2][24] = parseInt(tcType[newNumber][9] * 655.36 % 256);
          allData[2][25] = 0;
          allData[2][26] = 0;

          allData[2][27] = parseInt(tcType[newNumber][10] * 655.36 / 256);
          allData[2][28] = parseInt(tcType[newNumber][10] * 655.36 % 256);
          allData[2][29] = 0;
          allData[2][30] = 0;

          allData[2][31] = parseInt(tcType[newNumber][11] * 655.36 / 256);
          allData[2][32] = parseInt(tcType[newNumber][11] * 655.36 % 256);
          allData[2][33] = 0;
          allData[2][34] = 0;

          allData[2][35] = parseInt(tcType[newNumber][12] * 655.36 / 256);
          allData[2][36] = parseInt(tcType[newNumber][12] * 655.36 % 256);
          allData[2][37] = 0;
          allData[2][38] = 0;

          allData[2][39] = 0;
          allData[2][40] = 0;
          allData[2][41] = 0;
          allData[2][42] = 1;

          allData[2][43] = 0;
          allData[2][44] = 0;
          allData[2][45] = 0;
          allData[2][46] = 1;

          allData[2][47] = 0;
          allData[2][48] = 0;
          allData[2][49] = 0;
          allData[2][50] = 1;

          break;
      }
    } else if (selectedIndex == 1) {
      switch (index) {
        case 0:
          // 速度
          if (newNumber == 100) {
            allData[3][11] = 0;
            allData[3][12] = 0;
            allData[3][13] = 0;
            allData[3][14] = 1;
          } else {
            allData[3][11] = (newNumber * 2.56).toFixed(0);
            allData[3][13] = 0;
            allData[3][14] = 0;
          }
          break;
        case 1:
          if (newNumber == 100) {
            allData[3][7] = 0;
            allData[3][8] = 0;
            allData[3][9] = 0;
            allData[3][10] = 1;
          } else {
            allData[3][7] = (newNumber * 2.56).toFixed(0);
            allData[3][9] = 0;
            allData[3][10] = 0;
          }
          break;
        case 2:
          if (newNumber == 100) {
            allData[3][3] = 0;
            allData[3][4] = 0;
            allData[3][5] = 0;
            allData[3][6] = 1;
          } else {
            allData[3][3] = (newNumber * 2.56).toFixed(0);
            allData[3][5] = 0;
            allData[3][6] = 0;
          }
          // console.log('allData[1][3]:' + allData[1][3]);
          // allData[1][3] = 128; 
          break;
        case 3:
          if (newNumber == 100) {
            allData[3][15] = 0;
            allData[3][16] = 0;
            allData[3][17] = 0;
            allData[3][18] = 1;
          } else {
            allData[3][15] = (newNumber * 2.56).toFixed(0);
            allData[3][17] = 0;
            allData[3][18] = 0;
          }
          // allData[1][16] = newNumber;
          break;
        case 4:
          if (newNumber == 100) {
            allData[3][19] = 0;
            allData[3][20] = 0;
            allData[3][21] = 0;
            allData[3][22] = 1;
          } else {
            allData[3][19] = (newNumber * 2.56).toFixed(0);
            allData[3][21] = 0;
            allData[3][22] = 0;
          }
          // allData[1][20] = newNumber;
          break;
        case 5:
          allData[3][31] = newNumber;

          allData[3][35] = parseInt(tcType[newNumber][0] * 655.36 / 256);
          allData[3][36] = parseInt(tcType[newNumber][0] * 655.36 % 256);
          allData[3][37] = 0;
          allData[3][38] = 0;

          allData[3][39] = parseInt(tcType[newNumber][1] * 655.36 / 256);
          allData[3][40] = parseInt(tcType[newNumber][1] * 655.36 % 256);
          allData[3][41] = 0;
          allData[3][42] = 0;

          allData[3][43] = parseInt(tcType[newNumber][2] * 655.36 / 256);
          allData[3][44] = parseInt(tcType[newNumber][2] * 655.36 % 256);
          allData[3][45] = 0;
          allData[3][46] = 0;

          allData[3][47] = parseInt(tcType[newNumber][3] * 655.36 / 256);
          allData[3][48] = parseInt(tcType[newNumber][3] * 655.36 % 256);
          allData[3][49] = 0;
          allData[3][50] = 0;


          allData[4][3] = parseInt(tcType[newNumber][4] * 655.36 / 256);
          allData[4][4] = parseInt(tcType[newNumber][4] * 655.36 % 256);
          allData[4][5] = 0;
          allData[4][6] = 0;

          allData[4][7] = parseInt(tcType[newNumber][5] * 655.36 / 256);
          allData[4][8] = parseInt(tcType[newNumber][5] * 655.36 % 256);
          allData[4][9] = 0;
          allData[4][10] = 0;

          allData[4][11] = parseInt(tcType[newNumber][6] * 655.36 / 256);
          allData[4][12] = parseInt(tcType[newNumber][6] * 655.36 % 256);
          allData[4][13] = 0;
          allData[4][14] = 0;

          allData[4][15] = parseInt(tcType[newNumber][7] * 655.36 / 256);
          allData[4][16] = parseInt(tcType[newNumber][7] * 655.36 % 256);
          allData[4][17] = 0;
          allData[4][18] = 0;

          allData[4][19] = parseInt(tcType[newNumber][8] * 655.36 / 256);
          allData[4][20] = parseInt(tcType[newNumber][8] * 655.36 % 256);
          allData[4][21] = 0;
          allData[4][22] = 0;

          allData[4][23] = parseInt(tcType[newNumber][9] * 655.36 / 256);
          allData[4][24] = parseInt(tcType[newNumber][9] * 655.36 % 256);
          allData[4][25] = 0;
          allData[4][26] = 0;

          allData[4][27] = parseInt(tcType[newNumber][10] * 655.36 / 256);
          allData[4][28] = parseInt(tcType[newNumber][10] * 655.36 % 256);
          allData[4][29] = 0;
          allData[4][30] = 0;

          allData[4][31] = parseInt(tcType[newNumber][11] * 655.36 / 256);
          allData[4][32] = parseInt(tcType[newNumber][11] * 655.36 % 256);
          allData[4][33] = 0;
          allData[4][34] = 0;

          allData[4][35] = parseInt(tcType[newNumber][12] * 655.36 / 256);
          allData[4][36] = parseInt(tcType[newNumber][12] * 655.36 % 256);
          allData[4][37] = 0;
          allData[4][38] = 0;

          allData[4][39] = 0;
          allData[4][40] = 0;
          allData[4][41] = 0;
          allData[4][42] = 1;

          allData[4][43] = 0;
          allData[4][44] = 0;
          allData[4][45] = 0;
          allData[4][46] = 1;

          allData[4][47] = 0;
          allData[4][48] = 0;
          allData[4][49] = 0;
          allData[4][50] = 1;
          break;
      }
    } else if (selectedIndex == 2) {
      switch (index) {
        case 0:
          // 速度
          if (newNumber == 100) {
            allData[5][11] = 0;
            allData[5][12] = 0;
            allData[5][13] = 0;
            allData[5][14] = 1;
          } else {
            allData[5][11] = (newNumber * 2.56).toFixed(0);
            allData[5][13] = 0;
            allData[5][14] = 0;
          }
          break;
        case 1:
          if (newNumber == 100) {
            allData[5][7] = 0;
            allData[5][8] = 0;
            allData[5][9] = 0;
            allData[5][10] = 1;
          } else {
            allData[5][7] = (newNumber * 2.56).toFixed(0);
            allData[5][9] = 0;
            allData[5][10] = 0;
          }
          break;
        case 2:
          if (newNumber == 100) {
            allData[5][3] = 0;
            allData[5][4] = 0;
            allData[5][5] = 0;
            allData[5][6] = 1;
          } else {
            allData[5][3] = (newNumber * 2.56).toFixed(0);
            allData[5][5] = 0;
            allData[5][6] = 0;
          }
          // console.log('allData[1][3]:' + allData[1][3]);
          // allData[1][3] = 128; 
          break;
        case 3:
          if (newNumber == 100) {
            allData[5][15] = 0;
            allData[5][16] = 0;
            allData[5][17] = 0;
            allData[5][18] = 1;
          } else {
            allData[5][15] = (newNumber * 2.56).toFixed(0);
            allData[5][17] = 0;
            allData[5][18] = 0;
          }
          // allData[1][16] = newNumber;
          break;
        case 4:
          if (newNumber == 100) {
            allData[5][19] = 0;
            allData[5][20] = 0;
            allData[5][21] = 0;
            allData[5][22] = 1;
          } else {
            allData[5][19] = (newNumber * 2.56).toFixed(0);
            allData[5][21] = 0;
            allData[5][22] = 0;
          }
          // allData[1][20] = newNumber;
          break;
        case 5:
          allData[5][31] = newNumber;


          allData[5][35] = parseInt(tcType[newNumber][0] * 655.36 / 256);
          allData[5][36] = parseInt(tcType[newNumber][0] * 655.36 % 256);
          allData[5][37] = 0;
          allData[5][38] = 0;

          allData[5][39] = parseInt(tcType[newNumber][1] * 655.36 / 256);
          allData[5][40] = parseInt(tcType[newNumber][1] * 655.36 % 256);
          allData[5][41] = 0;
          allData[5][42] = 0;

          allData[5][43] = parseInt(tcType[newNumber][2] * 655.36 / 256);
          allData[5][44] = parseInt(tcType[newNumber][2] * 655.36 % 256);
          allData[5][45] = 0;
          allData[5][46] = 0;

          allData[5][47] = parseInt(tcType[newNumber][3] * 655.36 / 256);
          allData[5][48] = parseInt(tcType[newNumber][3] * 655.36 % 256);
          allData[5][49] = 0;
          allData[5][50] = 0;


          allData[6][3] = parseInt(tcType[newNumber][4] * 655.36 / 256);
          allData[6][4] = parseInt(tcType[newNumber][4] * 655.36 % 256);
          allData[6][5] = 0;
          allData[6][6] = 0;

          allData[6][7] = parseInt(tcType[newNumber][5] * 655.36 / 256);
          allData[6][8] = parseInt(tcType[newNumber][5] * 655.36 % 256);
          allData[6][9] = 0;
          allData[6][10] = 0;

          allData[6][11] = parseInt(tcType[newNumber][6] * 655.36 / 256);
          allData[6][12] = parseInt(tcType[newNumber][6] * 655.36 % 256);
          allData[6][13] = 0;
          allData[6][14] = 0;

          allData[6][15] = parseInt(tcType[newNumber][7] * 655.36 / 256);
          allData[6][16] = parseInt(tcType[newNumber][7] * 655.36 % 256);
          allData[6][17] = 0;
          allData[6][18] = 0;

          allData[6][19] = parseInt(tcType[newNumber][8] * 655.36 / 256);
          allData[6][20] = parseInt(tcType[newNumber][8] * 655.36 % 256);
          allData[6][21] = 0;
          allData[6][22] = 0;

          allData[6][23] = parseInt(tcType[newNumber][9] * 655.36 / 256);
          allData[6][24] = parseInt(tcType[newNumber][9] * 655.36 % 256);
          allData[6][25] = 0;
          allData[6][26] = 0;

          allData[6][27] = parseInt(tcType[newNumber][10] * 655.36 / 256);
          allData[6][28] = parseInt(tcType[newNumber][10] * 655.36 % 256);
          allData[6][29] = 0;
          allData[6][30] = 0;

          allData[6][31] = parseInt(tcType[newNumber][11] * 655.36 / 256);
          allData[6][32] = parseInt(tcType[newNumber][11] * 655.36 % 256);
          allData[6][33] = 0;
          allData[6][34] = 0;

          allData[6][35] = parseInt(tcType[newNumber][12] * 655.36 / 256);
          allData[6][36] = parseInt(tcType[newNumber][12] * 655.36 % 256);
          allData[6][37] = 0;
          allData[6][38] = 0;

          allData[6][39] = 0;
          allData[6][40] = 0;
          allData[6][41] = 0;
          allData[6][42] = 1;

          allData[6][43] = 0;
          allData[6][44] = 0;
          allData[6][45] = 0;
          allData[6][46] = 1;

          allData[6][47] = 0;
          allData[6][48] = 0;
          allData[6][49] = 0;
          allData[6][50] = 1;
          break;
      }
    } else if (selectedIndex == 3) {
      switch (index) {
        case 0:
          // 速度
          if (newNumber == 100) {
            allData[7][11] = 0;
            allData[7][12] = 0;
            allData[7][13] = 0;
            allData[7][14] = 1;
          } else {
            allData[7][11] = (newNumber * 2.56).toFixed(0);
            allData[7][13] = 0;
            allData[7][14] = 0;
          }
          break;
        case 1:
          if (newNumber == 100) {
            allData[7][7] = 0;
            allData[7][8] = 0;
            allData[7][9] = 0;
            allData[7][10] = 1;
          } else {
            allData[7][7] = (newNumber * 2.56).toFixed(0);
            allData[7][9] = 0;
            allData[7][10] = 0;
          }
          break;
        case 2:
          if (newNumber == 100) {
            allData[7][3] = 0;
            allData[7][4] = 0;
            allData[7][5] = 0;
            allData[7][6] = 1;
          } else {
            allData[7][3] = (newNumber * 2.56).toFixed(0);
            allData[7][5] = 0;
            allData[7][6] = 0;
          }
          // console.log('allData[1][3]:' + allData[1][3]);
          // allData[1][3] = 128; 
          break;
        case 3:
          if (newNumber == 100) {
            allData[7][15] = 0;
            allData[7][16] = 0;
            allData[7][17] = 0;
            allData[7][18] = 1;
          } else {
            allData[7][15] = (newNumber * 2.56).toFixed(0);
            allData[7][17] = 0;
            allData[7][18] = 0;
          }
          // allData[1][16] = newNumber;
          break;
        case 4:
          if (newNumber == 100) {
            allData[7][19] = 0;
            allData[7][20] = 0;
            allData[7][21] = 0;
            allData[7][22] = 1;
          } else {
            allData[7][19] = (newNumber * 2.56).toFixed(0);
            allData[7][21] = 0;
            allData[7][22] = 0;
          }
          // allData[1][20] = newNumber;
          break;
        case 5:
          allData[7][31] = newNumber;


          allData[7][35] = parseInt(tcType[newNumber][0] * 655.36 / 256);
          allData[7][36] = parseInt(tcType[newNumber][0] * 655.36 % 256);
          allData[7][37] = 0;
          allData[7][38] = 0;

          allData[7][39] = parseInt(tcType[newNumber][1] * 655.36 / 256);
          allData[7][40] = parseInt(tcType[newNumber][1] * 655.36 % 256);
          allData[7][41] = 0;
          allData[7][42] = 0;

          allData[7][43] = parseInt(tcType[newNumber][2] * 655.36 / 256);
          allData[7][44] = parseInt(tcType[newNumber][2] * 655.36 % 256);
          allData[7][45] = 0;
          allData[7][46] = 0;

          allData[7][47] = parseInt(tcType[newNumber][3] * 655.36 / 256);
          allData[7][48] = parseInt(tcType[newNumber][3] * 655.36 % 256);
          allData[7][49] = 0;
          allData[7][50] = 0;


          allData[8][3] = parseInt(tcType[newNumber][4] * 655.36 / 256);
          allData[8][4] = parseInt(tcType[newNumber][4] * 655.36 % 256);
          allData[8][5] = 0;
          allData[8][6] = 0;

          allData[8][7] = parseInt(tcType[newNumber][5] * 655.36 / 256);
          allData[8][8] = parseInt(tcType[newNumber][5] * 655.36 % 256);
          allData[8][9] = 0;
          allData[8][10] = 0;

          allData[8][11] = parseInt(tcType[newNumber][6] * 655.36 / 256);
          allData[8][12] = parseInt(tcType[newNumber][6] * 655.36 % 256);
          allData[8][13] = 0;
          allData[8][14] = 0;

          allData[8][15] = parseInt(tcType[newNumber][7] * 655.36 / 256);
          allData[8][16] = parseInt(tcType[newNumber][7] * 655.36 % 256);
          allData[8][17] = 0;
          allData[8][18] = 0;

          allData[8][19] = parseInt(tcType[newNumber][8] * 655.36 / 256);
          allData[8][20] = parseInt(tcType[newNumber][8] * 655.36 % 256);
          allData[8][21] = 0;
          allData[8][22] = 0;

          allData[8][23] = parseInt(tcType[newNumber][9] * 655.36 / 256);
          allData[8][24] = parseInt(tcType[newNumber][9] * 655.36 % 256);
          allData[8][25] = 0;
          allData[8][26] = 0;

          allData[8][27] = parseInt(tcType[newNumber][10] * 655.36 / 256);
          allData[8][28] = parseInt(tcType[newNumber][10] * 655.36 % 256);
          allData[8][29] = 0;
          allData[8][30] = 0;

          allData[8][31] = parseInt(tcType[newNumber][11] * 655.36 / 256);
          allData[8][32] = parseInt(tcType[newNumber][11] * 655.36 % 256);
          allData[8][33] = 0;
          allData[8][34] = 0;

          allData[8][35] = parseInt(tcType[newNumber][12] * 655.36 / 256);
          allData[8][36] = parseInt(tcType[newNumber][12] * 655.36 % 256);
          allData[8][37] = 0;
          allData[8][38] = 0;

          allData[8][39] = 0;
          allData[8][40] = 0;
          allData[8][41] = 0;
          allData[8][42] = 1;

          allData[8][43] = 0;
          allData[8][44] = 0;
          allData[8][45] = 0;
          allData[8][46] = 1;

          allData[8][47] = 0;
          allData[8][48] = 0;
          allData[8][49] = 0;
          allData[8][50] = 1;
          break;
      }
    }
  }

  const sleep = (ms) => {
    return new Promise(resolve => {
      setTimeout(() =>
        resolve(''), ms);
    })
  }

  // function sleep(ms: number) {
  //   return new Promise<void>(resolve => setTimeout(resolve, ms));
  // }

  async function setParaWithIndex() {
    // 根据index来设置
    if (!deviceId) {
      showMessage({
        message: 'Not connected',
        type: 'warning',
        icon: 'warning',
        duration: 2000,
      });
      return;
    }

    if (selectedIndex == 0) {
      // 写入 ECO1
      let tmpStr = (Array(4).join(0) + (9 * 24).toString(16)).slice(-4);
      let tmpData = '';
      for (let j = 0; j < 48; j++) {
        const element = allData[1][3 + j]; // parseInt(element).toString(16))
        tmpData += (Array(2).join(0) + parseInt(element).toString(16)).slice(-2);
      }
      var res = crc16(Buffer.from('0110' + tmpStr + '001830' + tmpData, "hex"));
      bleModule['write']('0110' + tmpStr + '001830' + tmpData + res[0] + res[1], 0).then(() => {
        // updateAllData([...allData]);
        // setIsTest(!isTest);
      }).catch(err => {
        showMessage({
          message: 'Save failed',
          type: 'warning',
          icon: 'warning',
          duration: 2000,
        });
      });

      await sleep(400);

      tmpStr = (Array(4).join(0) + (10 * 24).toString(16)).slice(-4);
      tmpData = '';
      for (let j = 0; j < 48; j++) {
        const element = allData[2][3 + j];
        tmpData += (Array(2).join(0) + parseInt(element).toString(16)).slice(-2);
      }
      res = crc16(Buffer.from('0110' + tmpStr + '001830' + tmpData, "hex"));
      bleModule['write']('0110' + tmpStr + '001830' + tmpData + res[0] + res[1], 0).then(() => {
        updateAllData([...allData]);
        setIsTest(!isTest);
        showMessage({
          message: 'Set successfully',
          type: 'success',
          icon: 'success',
          duration: 2000,
        });
      }).catch(err => {
        showMessage({
          message: 'Save failed',
          type: 'warning',
          icon: 'warning',
          duration: 2000,
        });
      });

      await sleep(200);
      bleModule['write']('0110021E000200000000009B8F', 0).then(() => {
        console.log('send save order');
      }).catch(err => {
        showMessage({
          message: 'Save failed',
          type: 'warning',
          icon: 'warning',
          duration: 2000,
        });
      });
      
    } else if (selectedIndex == 1) {
      let tmpStr = (Array(4).join(0) + (11 * 24).toString(16)).slice(-4);
      let tmpData = '';
      for (let j = 0; j < 48; j++) {
        const element = allData[3][3 + j];
        tmpData += (Array(2).join(0) + parseInt(element).toString(16)).slice(-2);
      }
      var res = crc16(Buffer.from('0110' + tmpStr + '001830' + tmpData, "hex"));
      bleModule['write']('0110' + tmpStr + '001830' + tmpData + res[0] + res[1], 0).then(() => {
        // updateAllData([...allData]);
        // setIsTest(!isTest);
      }).catch(err => {
        showMessage({
          message: 'Save failed',
          type: 'warning',
          icon: 'warning',
          duration: 2000,
        });
      });

      await sleep(400);

      tmpStr = (Array(4).join(0) + (12 * 24).toString(16)).slice(-4);
      tmpData = '';
      for (let j = 0; j < 48; j++) {
        const element = allData[4][3 + j];
        tmpData += (Array(2).join(0) + parseInt(element).toString(16)).slice(-2);
      }
      res = crc16(Buffer.from('0110' + tmpStr + '001830' + tmpData, "hex"));
      bleModule['write']('0110' + tmpStr + '001830' + tmpData + res[0] + res[1], 0).then(() => {
        updateAllData([...allData]);
        setIsTest(!isTest);
        showMessage({
          message: 'Set successfully',
          type: 'success',
          icon: 'success',
          duration: 2000,
        });
      }).catch(err => {
        showMessage({
          message: 'Save failed',
          type: 'warning',
          icon: 'warning',
          duration: 2000,
        });
      });

      await sleep(200);
      bleModule['write']('0110021E000200000000009B8F', 0).then(() => {
        console.log('send save order');
      }).catch(err => {
        showMessage({
          message: 'Save failed',
          type: 'warning',
          icon: 'warning',
          duration: 2000,
        });
      });
    } else if (selectedIndex == 2) {
      // 写入 ECO1
      let tmpStr = (Array(4).join(0) + (13 * 24).toString(16)).slice(-4);
      let tmpData = '';
      for (let j = 0; j < 48; j++) {
        const element = allData[5][3 + j];
        tmpData += (Array(2).join(0) + parseInt(element).toString(16)).slice(-2);
      }
      var res = crc16(Buffer.from('0110' + tmpStr + '001830' + tmpData, "hex"));
      bleModule['write']('0110' + tmpStr + '001830' + tmpData + res[0] + res[1], 0).then(() => {
        // updateAllData([...allData]);
        // setIsTest(!isTest);
      }).catch(err => {
        showMessage({
          message: 'Save failed',
          type: 'warning',
          icon: 'warning',
          duration: 2000,
        });
      });

      await sleep(400);

      tmpStr = (Array(4).join(0) + (14 * 24).toString(16)).slice(-4);
      tmpData = '';
      for (let j = 0; j < 48; j++) {
        const element = allData[6][3 + j];
        tmpData += (Array(2).join(0) + parseInt(element).toString(16)).slice(-2);
      }
      res = crc16(Buffer.from('0110' + tmpStr + '001830' + tmpData, "hex"));
      bleModule['write']('0110' + tmpStr + '001830' + tmpData + res[0] + res[1], 0).then(() => {
        updateAllData([...allData]);
        setIsTest(!isTest);
        showMessage({
          message: 'Set successfully',
          type: 'success',
          icon: 'success',
          duration: 2000,
        });
      }).catch(err => {
        showMessage({
          message: 'Save failed',
          type: 'warning',
          icon: 'warning',
          duration: 2000,
        });
      });

      await sleep(200);
      bleModule['write']('0110021E000200000000009B8F', 0).then(() => {
        console.log('send save order');
      }).catch(err => {
        showMessage({
          message: 'Save failed',
          type: 'warning',
          icon: 'warning',
          duration: 2000,
        });
      });
    } else if (selectedIndex == 3) {
      // 写入 ECO1
      let tmpStr = (Array(4).join(0) + (15 * 24).toString(16)).slice(-4);
      let tmpData = '';
      for (let j = 0; j < 48; j++) {
        const element = allData[7][3 + j];
        tmpData += (Array(2).join(0) + parseInt(element).toString(16)).slice(-2);
      }
      let res = crc16(Buffer.from('0110' + tmpStr + '001830' + tmpData, "hex"));
      bleModule['write']('0110' + tmpStr + '001830' + tmpData + res[0] + res[1], 0).then(() => {
        // updateAllData([...allData]);
        // setIsTest(!isTest);
      }).catch(err => {
        showMessage({
          message: 'Save failed',
          type: 'warning',
          icon: 'warning',
          duration: 2000,
        });
      });

      await sleep(400);

      tmpStr = (Array(4).join(0) + (16 * 24).toString(16)).slice(-4);
      tmpData = '';
      for (let j = 0; j < 48; j++) {
        const element = allData[8][3 + j];
        tmpData += (Array(2).join(0) + parseInt(element).toString(16)).slice(-2);
      }

      res = crc16(Buffer.from('0110' + tmpStr + '001830' + tmpData, "hex"));
      bleModule['write']('0110' + tmpStr + '001830' + tmpData + res[0] + res[1], 0).then(() => {
        updateAllData([...allData]);
        setIsTest(!isTest);
        showMessage({
          message: 'Set successfully',
          type: 'success',
          icon: 'success',
          duration: 2000,
        });
      }).catch(err => {
        showMessage({
          message: 'Save failed',
          type: 'warning',
          icon: 'warning',
          duration: 2000,
        });
      });
      
      await sleep(200);
      bleModule['write']('0110021E000200000000009B8F', 0).then(() => {
        console.log('send save order');
      }).catch(err => {
        showMessage({
          message: 'Save failed',
          type: 'warning',
          icon: 'warning',
          duration: 2000,
        });
      });
    }

  }

  return (
    <View style={{ flex: 1, alignItems: 'center', backgroundColor: '#DDDDDD' }}>
      {
        deviceId ? allData.length == 9 ? <View style={styles.container}>
          <SegmentedControlTab
            values={ settingMap[allData[0][24]] }
            // values={['ECO1', 'ECO2', 'ECO3', 'SPORT']}
            selectedIndex={selectedIndex}
            onTabPress={(index) => { setSelectedIndex(index) }}
            borderRadius={0}
            tabsContainerStyle={{ height: 50, backgroundColor: '#BEBEBE' }}
            tabStyle={{ backgroundColor: '#BEBEBE', borderWidth: 0, borderColor: 'transparent' }}
            activeTabStyle={{ backgroundColor: '#DDDDDD', margin: 2 }}
            tabTextStyle={styles.text}
            activeTabTextStyle={styles.text}
          />
          <SettingScrollView key={'item' + selectedIndex} allData={allData} selIndex={selectedIndex} sliderWithIndex={(index, newNumber) => sliderWithIndex(index, newNumber)} setParaWithIndex={() => setParaWithIndex()} />
        </View>
          : <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}><Text style={[styles.text, { fontSize: 20, lineHeight: 30 }]}>The controller version is not applicable to this app. Please go to the personal page to upgrade the controller first.</Text></View>
          : <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}><Text style={[styles.text, { fontSize: 20, lineHeight: 30 }]}>Please connect device {`\n`}from the main page</Text></View>
      }
    </View >
  )
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    marginTop: 70,
    width: '100%',
    backgroundColor: '#DDDDDD',
    padding: 10,
  },
  tabViewText: {
    color: '#444444',
    fontWeight: 'bold',
    marginTop: 50,
    fontSize: 18,
  },
  titleText: {
    color: '#444444',
    padding: 20,
    fontSize: 14,
    fontWeight: '500',
  },
  headerText: {
    padding: 8,
    fontSize: 14,
    color: '#444444',
  },
  text: {
    fontSize: 16,
    color: '#060606',
    fontWeight: "400",
    fontFamily: 'Moldiv'
  }

})

const mapStateToProps = ({
  device: {
    devName,
    deviceId,
    allData,
    kmStr
  },
}) => ({
  devName,
  deviceId,
  allData,
  kmStr
});

const enhance = compose(
  connect(mapStateToProps, {
    disConnectDev,
    updateAllData
  }),
);
export default enhance(SetPage);